package com.crm.biz.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.BizProjectLevel;
import com.crm.service.BizProjectLevelService;
import com.crm.util.ResponseUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/bizProjectLevel")
public class BizProjectLevelController {
	@Autowired
	private BizProjectLevelService bizProjectLevelService;
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<BizProjectLevel> page, BizProjectLevel t){
		
		Page<BizProjectLevel> list = (Page<BizProjectLevel>) bizProjectLevelService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/getAllOption")
	@ResponseBody
	public Map<String, Object> getAllOption(Page<BizProjectLevel> page, BizProjectLevel record){
		
		record.setEnable(1);
		
		Map<String, Object> map = new HashMap<>();
		record.setEnable(1);
		Page<BizProjectLevel> list = (Page<BizProjectLevel>) bizProjectLevelService.selectAll(page, record);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(BizProjectLevel record){
		
		Map<String, Object> map = new HashMap<>();
		
		if(ValidateUtil.isEmpty(record.getLevelTitle()))
			throw new RuntimeException("级别名称不能为空！");
		
		if(!ValidateUtil.isEmpty(record.getProjectLevelId())){
			bizProjectLevelService.updateByPrimaryKey(record);
		}else{
			bizProjectLevelService.insert(record);
		}
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(BizProjectLevel record){
		
		Map<String, Object> map = new HashMap<>();
		
		bizProjectLevelService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
}
